package finn.movingheadtracker.gui;

import java.awt.Dimension;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JSeparator;
import javax.swing.JTextField;

import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import finn.movingheadtracker.Cameraview;
import finn.movingheadtracker.Cameraviews;
import finn.movingheadtracker.Configuration;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JCheckBox;

import javax.swing.JScrollPane;

@SuppressWarnings("serial")
public class SettingsframeCamera extends JDialog implements MouseListener, ListSelectionListener{
	private JFrame SuperFrame;
	private JTextField tfres;
	private JTextField tfsource;
	private JTextField tfname;
	JList<Cameraview> list;
	JButton Selectbtn;
	JButton AddViewbtn;
	JButton DelViewbtn;	
	JButton Savebtn;
	JButton SavenClosebtn;
	JCheckBox chckbxshown;

	private Cameraviews views;
	private int editing_id;
	private JScrollPane scrollPane;
	
	public SettingsframeCamera(final JFrame SuperFrame){
		super(SuperFrame, "Einstellungen: Kameraperspektiven");
		this.SuperFrame = SuperFrame;
		views = Configuration.views;
		Cameraview[] viewlist = views.list.toArray(new Cameraview[views.list.size()]);
		
		JLabel ViewsHeadlbl = new JLabel("Perspektiven");
		
		JLabel Propertieslbl = new JLabel("Eigenschaften");
		
		JLabel lblName = new JLabel("Name");
		
		JLabel lblSource = new JLabel("Source");
		
		JLabel lblResolution = new JLabel("Resolution");
		
		JSeparator separator = new JSeparator();
		separator.setOrientation(SwingConstants.VERTICAL);
		
		tfres = new JTextField();
		tfres.setColumns(10);
		
		tfsource = new JTextField();
		tfsource.setColumns(10);
		
		tfname = new JTextField();
		tfname.setColumns(10);
		
		Selectbtn = new JButton("Ausw\u00E4hlen");
		Selectbtn.addMouseListener(this);
		
		AddViewbtn = new JButton("Hinzuf\u00FCgen");
		AddViewbtn.addMouseListener(this);
		
		DelViewbtn = new JButton("Entfernen");
		DelViewbtn.addMouseListener(this);
		
		Savebtn = new JButton("Speichern");
		Savebtn.addMouseListener(this);
		
		SavenClosebtn = new JButton("Speichern und Schlie\u00DFen");
		SavenClosebtn.addMouseListener(this);
		
		chckbxshown = new JCheckBox("Anzeigen");
		
		scrollPane = new JScrollPane();
		
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
							.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
								.addComponent(AddViewbtn)
								.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(DelViewbtn, GroupLayout.PREFERRED_SIZE, 98, GroupLayout.PREFERRED_SIZE))
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(ViewsHeadlbl)
								.addGap(49)))
						.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 193, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(separator, GroupLayout.PREFERRED_SIZE, 1, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(Propertieslbl)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblSource)
								.addComponent(lblName)
								.addComponent(lblResolution)
								.addComponent(Savebtn))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(tfsource, GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(Selectbtn, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE))
								.addComponent(SavenClosebtn, GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
								.addComponent(tfname, GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(tfres, GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(chckbxshown)))))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(ViewsHeadlbl)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 86, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(DelViewbtn)
								.addComponent(AddViewbtn)))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(Propertieslbl)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblName)
								.addComponent(tfname, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblSource)
								.addComponent(tfsource, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(Selectbtn, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblResolution)
								.addComponent(tfres, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(chckbxshown))
							.addGap(15)
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(Savebtn)
								.addComponent(SavenClosebtn)))
						.addComponent(separator, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		list = new JList<Cameraview>();
		scrollPane.setViewportView(list);
		list.addListSelectionListener(this);
		list.setListData(viewlist);
		getContentPane().setLayout(groupLayout);
		this.setPreferredSize(new Dimension(600,210));
		pack();
		setVisible(true);
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		if(arg0.getSource()==Selectbtn){
			
		}
		else if(arg0.getSource()==AddViewbtn){
			Cameraview view = new Cameraview();
			view.id = views.getNextId();
			view.name = "Default Name";
			view.source = "HD Webcam";
			view.sourcetype = "webcam";
			view.res = new Dimension(640,360);
			view.used = false;
			views.addElement(view);
			list.setListData(views.getList().toArray(new Cameraview[views.getList().size()]));
			list.updateUI();
			list.setSelectedIndex(list.getFirstVisibleIndex());
		}
		else if(arg0.getSource()==DelViewbtn){
			if(list.getSelectedValue() instanceof Cameraview){
				int selected = list.getSelectedIndex();
				views.list.remove(list.getSelectedValue());
				list.setListData(views.getList().toArray(new Cameraview[views.getList().size()]));
				list.updateUI();
				if(selected == 0){
					list.setSelectedValue(0, true);
				}
				else{
					list.setSelectedValue(selected-1,true);
				}
			}
			else{
				//TODO Add nothing selceted exception
				new JDialog(SuperFrame, "hello");
			}
		}
		else if(arg0.getSource()==Savebtn){
			int selected = list.getSelectedIndex();
			Cameraview toRemove = views.getCameraview(editing_id);
			views.removeElement(toRemove);
			Cameraview toAdd = new Cameraview();
			toAdd.name = tfname.getText();
			toAdd.id = editing_id;
			toAdd.used = this.chckbxshown.isSelected();
			String[] splitres = tfsource.getText().split(":",2);
			toAdd.sourcetype = splitres[0];
			toAdd.source = splitres[1];
			toAdd.setStringResolution(tfres.getText());
			views.addElement(toAdd);
			list.setListData(views.getList().toArray(new Cameraview[views.getList().size()]));
			list.updateUI();
			list.setSelectedIndex(selected);
			
		}
		else if(arg0.getSource()==SavenClosebtn){
			int selected = list.getSelectedIndex();
			Cameraview toRemove = views.getCameraview(editing_id);
			views.removeElement(toRemove);
			Cameraview toAdd = new Cameraview();
			toAdd.name = tfname.getText();
			toAdd.id = editing_id;
			toAdd.used = this.chckbxshown.isSelected();
			String[] splitres = tfsource.getText().split(":",2);
			toAdd.sourcetype = splitres[0];
			toAdd.source = splitres[1];
			toAdd.setStringResolution(tfres.getText());
			views.addElement(toAdd);
			list.setListData(views.getList().toArray(new Cameraview[views.getList().size()]));
			list.updateUI();
			list.setSelectedIndex(selected);
			close();
		}
	}

	public void close(){
		Configuration.views = views;
		this.dispose();
	}
	@Override
	public void mouseEntered(MouseEvent arg0) {}

	@Override
	public void mouseExited(MouseEvent arg0) {}

	@Override
	public void mousePressed(MouseEvent arg0) {}

	@Override
	public void mouseReleased(MouseEvent arg0) {}

	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		if(list.getSelectedValue() instanceof Cameraview){
			editing_id = list.getSelectedValue().id;
			this.chckbxshown.setSelected(list.getSelectedValue().used);
			this.tfname.setText(list.getSelectedValue().name);
			this.tfsource.setText(list.getSelectedValue().sourcetype + ":" + list.getSelectedValue().source);
			this.tfres.setText(list.getSelectedValue().getStringResolution());
		}
	}
}